 const http = require('http');
 var axios = require('axios');
 const hostname = '127.0.0.1';
 const port = 3000;
 var UUID = require('uuid');
 const crypto=require('crypto');
 var URL = require('url');
 var urlencode = require('urlencode');


 const server = http.createServer(function(req, res) {
    // if(req.url == "/" && req.method.toLowerCase() =="post") {
        //res.end('Hello, World! TOM\n');
        
        
        var arg = URL.parse(req.url).query;
        var name = "";
        var mobile = "";
        var dateFrom = "";
        var dateTo = "";
        var sex = "";
        var clinic = "";
        var teethpro = new Array();
        var demonds = "";
        
        var params = arg.split("&");
        console.log(params);
        for(var i=0;i<params.length;i++)
        {
            var myitem = params[i].split("=");
            
            if (myitem[0] == "name") {
                name = urlencode.decode(myitem[1], 'utf-8');      //姓名
            } else if (myitem[0] == "mobile") {
                mobile = myitem[1];    //电话
            } else if (myitem[0] == "dateFrom") {
                dateFrom = myitem[1];
            } else if (myitem[0] == "dateTo") {
                dateTo = myitem[1];
            } else if (myitem[0] == "sex") {
                sex = myitem[1];
            } else if (myitem[0] == "age") {
                age = myitem[1];
            } else if (myitem[0] == "clinic") {
                clinic = urlencode.decode(myitem[1], 'utf-8');
            } else if (myitem[0] == "teethpro") {
                teethpro = myitem[1];
            } else if (myitem[0] == "demonds") {
                demonds = urlencode.decode(myitem[1], 'utf-8');
            } 
                
        }
        console.log(teethpro);
        console.log(demonds);
        
        let requestData = {
            email: '2998968@qq.com',
            password: 'daxi@2021'
        };
        let url = 'https://1348505.s4.udesk.cn/open_api_v1/log_in';
        let token = "";
        
        axios.post(url, requestData)
        .then(function (response) {
            //console.log(response);
            // res.end(response.data.open_api_auth_token);
            token = response.data.open_api_auth_token;
        })
        .then(function (response) {
            let open_api_auth_token;
            let email = "2998968@qq.com";
            let a = Date.parse(new Date());//获取当前时间
            let timestamp = a/1000; //获取时间戳
            let sign_version = "v2";
            
            let nonce = UUID.v1(); //获取随机字符串
            
            let userpass = email + "&" + token + "&" + timestamp + "&" + nonce + "&" + sign_version;
            let sign = crypto.createHash('SHA256').update(userpass).digest('hex');
            
            let addCustomerUrl = 'https://1348505.s4.udesk.cn/open_api_v1/customers?' 
                                 + 'email=' + email 
                                 + '&timestamp=' + timestamp 
                                 + '&nonce=' + nonce 
                                 + '&sign=' + sign
                                 + '&sign_version=' + sign_version;
            
            
            let customerInfo = {
              "customer": {
                "email": "",
                "nick_name": name,
                "cellphones": [
                    [null, mobile]
                ],
                "custom_fields": {
    	            "SelectField_3787": sex,//0：女、1:男
    	            "SelectField_3788": age,//0：1019、1：2029、2：3039、3：4049、4：5059、5：60+
    	            "TextField_7411": "第一期望时间: " + dateFrom + "第二期望时间: " + dateTo,
    	            "TextField_8098": clinic,
    	            "TextField_22677":teethpro,
    	            "TextField_22625":demonds
            	}
              }
            };
            
            
            
            console.log('addCustomerUrl:' + addCustomerUrl);
            console.log(JSON.stringify(customerInfo, null, 4));

            axios.post(addCustomerUrl,customerInfo)
             .then(function (response) {
                 res.end(JSON.stringify(response));
             })
             .catch(function (error) {
                 res.end(JSON.stringify(error));
             });
        })
        .catch(function (error) {
            res.end(error);
        });
 });
    
 server.listen(port, hostname, function() {
   console.log("Server is running at %s:%s", hostname, port);
 });